package com.example.new1.views;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.provider.MediaStore;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;


import com.example.new1.R;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;

public class MultiTouchView extends View {
    private static final String TAG = "MultiSignatureView";
    private Path[] mPaths = new Path[2];
    private Paint mPaint = new Paint();
    private Canvas mCanvas;
    private Bitmap mCacheBitmap;
    private Context mContext;

    public MultiTouchView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(10);
        mPaint.setColor(getResources().getColor(R.color.red, null));

        mPaths[0] = new Path();
        mPaths[1] = new Path();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mCacheBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mCacheBitmap);
        mCanvas.drawColor(Color.WHITE);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int pointerCount = event.getPointerCount();
        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_POINTER_DOWN:
                for (int i = 0; i < pointerCount; i++) {
                    int id = event.getPointerId(i);
                    if (id == 0 || id == 1) {
//                        mPaths[id].reset();
                        mPaths[id].moveTo(event.getX(i), event.getY(i));
                        Log.e(TAG, "ACTION_DOWN getX = " + event.getX(i) + " getY = " + event.getY(i));
                    }
                }
                break;
            case MotionEvent.ACTION_MOVE:
                for (int i = 0; i < pointerCount; i++) {
                    int id = event.getPointerId(i);
                    if (id == 0 || id == 1) {
                        mPaths[id].lineTo(event.getX(i), event.getY(i));
                        Log.e(TAG, "ACTION_MOVE getX = " + event.getX(i) + " getY = " + event.getY(i));
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_POINTER_UP:
                for (int i = 0; i < pointerCount; i++) {
                    int id = event.getPointerId(i);
                    if (id == 0 || id == 1) {
                        Log.e(TAG, "ACTION_UP getX = " + event.getX(i) + " getY = " + event.getY(i));
                    }
                }
                break;
        }

        mCanvas.drawPath(mPaths[0], mPaint);
        mCanvas.drawPath(mPaths[1], mPaint);
        invalidate();
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.e(TAG, "onDraw canvas = " + canvas);
        canvas.drawPath(mPaths[0], mPaint);
        canvas.drawPath(mPaths[1], mPaint);
    }

    public void clear() {
        Log.e(TAG, "clear()");
        mPaths[0].reset();
        mPaths[1].reset();
        mCanvas.drawColor(Color.WHITE);
        invalidate();
    }

    public void save() {
        File file = new File(mContext.getExternalFilesDir(null), "testSign.png");
        if (file.exists()) {
            file.delete();
        }
        OutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Toast.makeText(mContext, "保存异常：" + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
        mCacheBitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
        Toast.makeText(mContext, "保存成功! path:" + file.getAbsolutePath(), Toast.LENGTH_SHORT).show();
    }

    public void saveImageToGallery() {
        String savedImageURL = MediaStore.Images.Media.insertImage(
                mContext.getContentResolver(),
                mCacheBitmap,
                "testSign",
                "testSign save"
        );
        Toast.makeText(mContext, "保存成功! URL:" + savedImageURL, Toast.LENGTH_SHORT).show();
    }
}